package com.sangfor.ssl.service.utils;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import com.esri.core.geometry.ShapeModifiers;
import com.sangfor.bugreport.logger.Log;
import com.sangfor.ssl.rdp.utils.crypto.Crypto;
import com.sangfor.ssl.service.setting.ResourceManager;
import com.sangfor.ssl.service.setting.SettingManager;
import com.sangfor.ssl.service.setting.SystemConfiguration;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Locale;

/* loaded from: classes2.dex */
public class Common {
    private static final String TAG = "Common";
    private static String country = null;
    private static boolean hasVisit = false;
    private static String language = null;
    private static boolean languageChange = false;

    /* loaded from: classes2.dex */
    public static class AssertionFailed extends Error {
        public AssertionFailed(String str) {
            super(str);
        }
    }

    public static boolean hasVisit() {
        return hasVisit;
    }

    public static void setVisit(boolean z) {
        hasVisit = z;
    }

    public static void ASSERT(boolean z, String str) {
        if (!z) {
            throw new AssertionFailed(str);
        }
    }

    public static boolean isChinese() {
        return Locale.getDefault().getLanguage().equals(Locale.CHINA.getLanguage()) && Locale.getDefault().getCountry().equals(Locale.CHINA.getCountry());
    }

    public static boolean isLanguageChange() {
        String str;
        String str2;
        String language2 = Locale.getDefault().getLanguage();
        String country2 = Locale.getDefault().getCountry();
        if (languageChange || (((str = language) != null && !str.equals(language2)) || ((str2 = country) != null && !str2.equals(country2)))) {
            languageChange = true;
        }
        language = language2;
        country = country2;
        return languageChange;
    }

    public static void restartThinEC(Context context, Class<?> cls) {
        Intent intent = new Intent(context, cls);
        intent.setFlags(ShapeModifiers.ShapeHasTextures);
        context.startActivity(intent);
        System.exit(0);
    }

    public static boolean settingIsAbleWork() {
        return SettingManager.isAbleWork && ResourceManager.isAbleWork && SystemConfiguration.isAbleWork;
    }

    public static boolean StringIsEmpty(String str) {
        return str == null || str.charAt(0) == 0;
    }

    public static int toInt(String str) {
        if (str == null) {
            return 0;
        }
        char[] charArray = str.toCharArray();
        int i = 0;
        for (int i2 = 0; i2 < charArray.length; i2++) {
            if (charArray[i2] >= '0' && charArray[i2] <= '9') {
                i = ((i * 10) + charArray[i2]) - 48;
            }
        }
        return '-' == charArray[0] ? 0 - i : i;
    }

    private static boolean suportMinVersion(String[] strArr, String[] strArr2) {
        if (strArr == null || strArr2 == null) {
            return false;
        }
        int length = strArr.length > strArr2.length ? strArr2.length : strArr.length;
        for (int i = 0; i < length; i++) {
            int i2 = toInt(strArr2[i]);
            int i3 = toInt(strArr[i]);
            if (i3 > i2) {
                return true;
            }
            if (i3 < i2) {
                return false;
            }
        }
        return strArr2.length <= strArr.length;
    }

    public static int updateVersion(String str, String str2, String str3) {
        if (str == null || str.equals("") || str2 == null || str2.equals("")) {
            return 2;
        }
        int i = 3;
        if (str3 != null && !str3.equals("")) {
            String[] split = str.split("\\.");
            String[] split2 = str3.split("\\.");
            if (!suportMinVersion(split2, str2.split("\\."))) {
                return 3;
            }
            int length = split2.length > split.length ? split.length : split2.length;
            int i2 = 0;
            while (true) {
                i = 1;
                if (i2 < length) {
                    int i3 = toInt(split[i2]);
                    int i4 = toInt(split2[i2]);
                    if (i3 > i4) {
                        return 1;
                    }
                    if (i3 < i4) {
                        return 2;
                    }
                    i2++;
                } else if (split2.length > split.length) {
                    return 2;
                }
            }
        }
        return i;
    }

    public static int versionToInt(String str) {
        if (str.equals("6.2.2.0")) {
            return 9;
        }
        if (str.equals(IGeneral.VPN_VERSION_60_VDI_20)) {
            return 8;
        }
        if (str.equals(7)) {
            return 7;
        }
        if (str.equals(IGeneral.VPN_VERSION_60)) {
            return 6;
        }
        if (str.equals("5.8.0.0")) {
            return 5;
        }
        if (str.equals(IGeneral.VPN_VERSION_56)) {
            return 4;
        }
        if (str.equals(IGeneral.VPN_VERSION_55)) {
            return 3;
        }
        if (str.equals(IGeneral.VPN_VERSION_53)) {
            return 2;
        }
        return str.equals("5.2.1.0") ? 1 : 0;
    }

    public static int mbStringLength(String str) {
        int i = 0;
        for (int i2 = 0; i2 < str.length(); i2++) {
            int codePointAt = str.codePointAt(i2);
            if (codePointAt < 127) {
                i++;
            } else if (128 <= codePointAt && codePointAt <= 2047) {
                i += 2;
            } else if (2048 <= codePointAt && codePointAt <= 65535) {
                i += 3;
            }
        }
        return i;
    }

    public static byte[] encrypt(String str, String str2) {
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2)) {
            return new byte[0];
        }
        try {
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(str.getBytes());
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            new Crypto(str2).encrypt(byteArrayInputStream, byteArrayOutputStream);
            return byteArrayOutputStream.toByteArray();
        } catch (Exception e) {
            Log.error(TAG, "encrypt fail", e);
            return new byte[0];
        }
    }

    public static String decrypt(byte[] bArr, String str) {
        try {
            Crypto crypto = new Crypto(str);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bArr);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            crypto.decrypt(byteArrayInputStream, byteArrayOutputStream);
            return byteArrayOutputStream.toString();
        } catch (Exception e) {
            Log.error(TAG, "decrypt fail", e);
            return "";
        }
    }
}
